package com.transcendence.appwidget.twothree;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import com.transcendence.appwidget.R;
import com.transcendence.appwidget.activity.EditActivity;
import com.transcendence.appwidget.twofive.TwoFiveAppWidget;
import com.transcendence.appwidget.updatetime.UpdateTimeService;
import com.transcendence.core.utils.date.DateUtils;
import com.transcendence.core.utils.log.LogUtils;
import com.transcendence.core.utils.sp.SPUtils;

/**
 * @author joephone
 * @date 2023/9/14
 * @desc   AppWidgetProvider类本质就是一个广播接收者，通过源码我们可以看它继承了BroadcastReceiver类。
 */
public class TwoThreeAppWidget extends AppWidgetProvider {

    Context mContext;

    private void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                 int appWidgetId) {
////        CharSequence widgetText = "WIDEGET";
//        // Construct the RemoteViews object
//        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_two_three_nocar);
////        views.setTextViewText(R.id.textView, widgetText);
//        // Instruct the widget manager to update the widget
//        appWidgetManager.updateAppWidget(appWidgetId, views);

    }
    /*
     * 每次窗口小部件被更新都调用一次该方法
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        mContext = context;
        // There may be multiple widgets active, so update all of them
//        for (int appWidgetId : appWidgetIds) {
//            updateAppWidget(context, appWidgetManager, appWidgetId);
//        }
        updateWidgetView(context,appWidgetManager , DateUtils.getInstance().formatDate2String(System.currentTimeMillis()+""),appWidgetIds);
    }
    /*
     * 接收窗口小部件点击时发送的广播
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
    /*
     * 当小部件从备份恢复时调用该方法
     */
    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }

    /*
     * 每删除一次窗口小部件就调用一次
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    /*
     * 当该窗口小部件第一次添加到桌面时调用该方法
     */
    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
        context.startService(new Intent(context, UpdateTimeService.class));
    }
    /*
     * 当最后一个该窗口小部件删除时调用该方法
     */
    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
//        context.stopService(new Intent(context,UpdateTimeService.class));
    }
    /*
     * 当小部件大小改变时
     */
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    /**
     * 更新桌面小组件数据用，APP中也可以在任意地方传入任意数据进来主动更新小组件数据
     */
    public void updateWidgetView(Context context, AppWidgetManager appWidgetManager, String text,  int[] appWidgetIds) {
        LogUtils.d("updateWidgetView");
        //初始化RemoteViews
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_two_three);
        ComponentName componentName = new ComponentName(context, TwoFiveAppWidget.class);
        updateRemoteViewImage(remoteViews,componentName,context);

        //注册点击事件
        remoteViews.setOnClickPendingIntent(
                R.id.tv_edit,
                registerEditAction(context)
        );


        for (int appWidgetId : appWidgetIds) {
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    private void updateRemoteViewImage(RemoteViews remoteViews, ComponentName componentName, Context context) {
        int resourceId = R.drawable.bg_app_widget_gradient_default;
        int selected = SPUtils.getInstance().getInt("AppWidgetBgSelected",0);
        switch (selected){
            case 0:
                resourceId = R.drawable.bg_app_widget_gradient_default;
                break;
            case 1:
                resourceId = R.drawable.bg_app_widget_gradient_one;
                break;
            case 2:
                resourceId = R.drawable.bg_app_widget_gradient_two;
                break;
            case 3:
                resourceId = R.drawable.bg_app_widget_gradient_three;
                break;
            case 4:
                resourceId = R.drawable.bg_app_widget_gradient_four;
                break;
        }
        remoteViews.setImageViewResource(
                R.id.bg_appwidget,
                resourceId
        );
        AppWidgetManager.getInstance(context)
                .updateAppWidget(componentName, remoteViews);
    }

    private PendingIntent registerEditAction(Context context) {
//        SPUtils.getInstance().save("goto_edit",true);
        Intent intent = new Intent(context, EditActivity.class);
        intent.putExtra("path","edit");
        Bundle bundle = new Bundle();
        bundle.putString("appWidgetPath","edit");
        intent.putExtras(bundle);
        return PendingIntent.getActivity(context,3,intent,PendingIntent.FLAG_UPDATE_CURRENT,bundle);
    }

}
